#version 460 core
#extension GL_GOOGLE_include_directive : enable

#include "../../../../core/shader_color.h"
#include "../../../../core/shader_curve.h"
#include "../../../../core/shader_line.h"

#include "../../../../core/shader_taster_curve.h"
//#include "../../../../core/shader_widget_panel.h"
//#include "../../../../core/shader_widget_color.h"


 
layout(points) in;
layout(line_strip, max_vertices=64) out;
//layout(lines, max_vertices=128) out;





layout(location = 0) in VS_GS_VERTEX {
	uint InstanceIndex;
}vertex_in[];



layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
};







void main(void){
	uint instance = vertex_in[0].InstanceIndex;
	
	S_CurvePoint curve = m_Curve[instance];
	
	vec2 size = curve.R_point - curve.L_point;
	if(length(size) < 3) return;

	//float al = min(curve.value.x, min(abs(size.x*0.5), abs(size.y*0.5)));
	float al = f_glsl_Cal_CutCircleRadius(curve);

	vec4 TColor = f_glsl_buildColor(curve.ID_Color.y);
	
	

	vec2 begin;
	vec2 end = curve.R_point;
	vec2 perpendicular;
	
	float PN;
	vec4 v = vec4(0.0, 0.0, 0.0, 1.0);

	float Inv = curve.value.x;
	float angle = f_glsl_Cal_CutCircleNum(curve.L_point, end, int(curve.Select_Type.y), al, Inv);
	switch(curve.Select_Type.y){
	case DEF_LINKLINE_TYPE_CircleTangent_V : {
		PN = sign(size.x);

		uint num = uint(degrees(angle) / 6);
		float a = M_PI / 180 * 6;
		
		gl_Position = vec4(curve.L_point, 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		gColor = TColor;
		EmitVertex();
		

		for(int i=0; i<num; ++i){
			v.x = (-cos(a*i) * al + al) * PN;
			v.y = sin(a*i) * al;
		
			gl_Position = v;
			gl_Position.xy += curve.L_point;
			gl_Position.xy *= m_Scale;
			gl_Position.xy += m_Loc;
			gl_Position = mat_ProjecView * gl_Position;
			gColor = TColor;
			EmitVertex();
		}
		
		for(int i=0; i<=num; ++i){
			float index = num - i - 1;
			v.x = (cos(a*index) * al - al) * PN * Inv;
			v.y = sin(a*index) * -al;
		
			gl_Position = v;
			gl_Position.xy += curve.R_point;
			gl_Position.xy *= m_Scale;
			gl_Position.xy += m_Loc;
			gl_Position = mat_ProjecView * gl_Position;
			gColor = TColor;
			EmitVertex();
		}
		break;
	}
		
	case DEF_LINKLINE_TYPE_CircleTangent_H : {
		PN = sign(size.y);

		uint num = uint(degrees(angle) / 4);
		float a = M_PI / 180 * 4;
		
		gl_Position = vec4(curve.L_point, 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		gColor = TColor;
		EmitVertex();
		
		

		for(int i=0; i<num; ++i){
			v.x = sin(a*i) * al;
			v.y = (-cos(a*i) * al + al) * PN;
			
			gl_Position = v;
			gl_Position.xy += curve.L_point;
			gl_Position.xy *= m_Scale;
			gl_Position.xy += m_Loc;
			gl_Position = mat_ProjecView * gl_Position;
			gColor = TColor;
			EmitVertex();
		}
		
		for(int i=0; i<=num; ++i){
			float index = num - i - 1;
			v.x = sin(a*index) * -al;
			v.y = (cos(a*index) * al - al) * PN * Inv;
			
			gl_Position = v;
			gl_Position.xy += curve.R_point;
			gl_Position.xy *= m_Scale;
			gl_Position.xy += m_Loc;
			gl_Position = mat_ProjecView * gl_Position;
			gColor = TColor;
			EmitVertex();
		}
		break;
	}

	case DEF_LINKLINE_TYPE_C : {
		PN = sign(size.y);

		gl_Position = vec4(curve.L_point, 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		gColor = TColor;
		EmitVertex();

		v.xy = curve.L_point;
		v.x = min(curve.L_point.x, curve.R_point.x) - 20;
		gl_Position = vec4(v.xy, 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		gColor = TColor;
		EmitVertex();

		v.y = curve.R_point.y;
		gl_Position = vec4(v.xy, 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		gColor = TColor;
		EmitVertex();

		v.x = curve.R_point.x;
		gl_Position = vec4(v.xy, 0, 1);
		gl_Position.xy *= m_Scale;
		gl_Position.xy += m_Loc;
		gl_Position = mat_ProjecView * gl_Position;
		gColor = TColor;
		EmitVertex();
		break;
	}
	}

	


	//v.xy = normalize(-vec2(size));
	gl_Position = vec4(curve.R_point, 0, 1);
	gl_Position.xy *= m_Scale;
	gl_Position.xy += m_Loc;
	gl_Position = mat_ProjecView * gl_Position;
	gColor = TColor;
	EmitVertex();


	//gl_Position = vec4(perpendicular*30 + begin, 0, 1);
	//gl_Position.xy *= m_Scale;
	//gl_Position.xy += m_Loc;
	//gl_Position = mat_ProjecView * gl_Position;
	//gColor = TColor;
	//EmitVertex();


	//gl_Position = vec4(begin, 0, 1);
	//gl_Position.xy *= m_Scale;
	//gl_Position.xy += m_Loc;
	//gl_Position = mat_ProjecView * gl_Position;
	//gColor = TColor;
	//EmitVertex();


	EndPrimitive();
}


 